using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;

namespace EMailObfuscator
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void richTextBox1_TextChanged(object sender, EventArgs e)
		{
			richTextBox2.Text = Obfuscate(richTextBox1.Text);
		}

		private static Regex email = new Regex("<a href=\"mailto:([^>\"]+)\">([^<>]+)</a>");
		private string Obfuscate(string p)
		{
			string result = p;

			MatchCollection results = email.Matches(p);
			foreach (Match m in results)
			{
				string all = m.Groups[0].Value;
				string mail = m.Groups[1].Value;
				result = result.Replace(all, ObfuscateMail(mail));
				//ObfuscateMail(ref result, mail);
				//mail = m.Groups[2].Value;
				//ObfuscateMail(ref result, mail);
			}

			return result;
		}

		public void ObfuscateMail(ref string sourceText, string mail)
		{
			sourceText = sourceText.Replace(mail, ObfuscateMail(mail));
		}

		public string ObfuscateMail(string mail)
		{
			StringBuilder result = new StringBuilder();
			foreach (char c in mail)
			{
				result.Append("&#");
				result.Append(((int)c).ToString());
				result.Append(";");
			}
			return result.ToString();
		}
	}
}